# ################################################################ #
# include.make                                                     #
#                                                                  #
# One Makefile To Rule Them All                                    #
#                                                                  #
# (c) 2003 Wouter Caarls, Delft University of Technology           #
# ################################################################ #


ifndef PARALLEL
.NOTPARALLEL:
endif

-include $(SHARE)/vars.make

NAME = $(strip $(META) $(C_PROGRAM) $(CPP_PROGRAM) $(C_LIBRARY) $(CPP_LIBRARY) $(FORTRAN_PROGRAM) $(LATEX_DOCUMENT))

# Main directory manipulation #################################### #

ifeq ($(words $(NAME)), 1)
  ifdef META
    include $(SHARE)/meta.make
  endif
  ifdef CPP_PROGRAM
    LINKER:=$(CPP_LINKER)
    include $(SHARE)/c.make
  endif
  ifdef C_PROGRAM
    LINKER:=$(C_LINKER)
    include $(SHARE)/c.make
  endif
  ifdef CPP_LIBRARY
    LINKER:=$(CPP_LINKER)
    include $(SHARE)/c.lib.make
  endif
  ifdef C_LIBRARY
    LINKER:=$(C_LINKER)
    include $(SHARE)/c.lib.make
  endif
  ifdef FORTRAN_PROGRAM
    include $(SHARE)/fortran.make
  endif
  ifdef LATEX_DOCUMENT
    include $(SHARE)/latex.make
  endif
else
  $(error No, or multiple, make types set)
endif

# Set the distribution dir if not already defined (normally on commandline)
DISTDIR ?= $(shell pwd)/$(NAME)-$(MAJOR).$(MINOR).$(REVISION)

# Set the relative path to the SHARE directory within the distribution
SHAREDISTDIR ?= .

# The SHARE variable in the Makefile is replaced with a sed command (during dist)
# transform the relative path to something that sed can parse later on
SEDSHAREDISTDIR = $(shell echo $(SHAREDISTDIR) | sed -e "s/\//\\\\\//g"  )

# General Subdirectory manipulation ############################## #

SUBTARGETS += all clean install uninstall

define SUBTARGET_template
$$(filter-out $$(OVERRIDE_TARGETS), subdirs.$(1)): $$(SUBDIRS:%=%.$(1)) ;
$$(filter-out $$(OVERRIDE_TARGETS), $$(SUBDIRS:%=%.$(1))): %.$(1): force
	$$(MAKE) -C $$* $(1)
TEMPLATE_INSTANTIATION = working
endef

$(foreach subtarget,$(SUBTARGETS),$(eval $(call SUBTARGET_template,$(subtarget))))

ifndef TEMPLATE_INSTANTIATION
# Obsolete make version
$(filter-out $(OVERRIDE_TARGETS), subdirs.all): $(SUBDIRS:%=%.all) ;
$(filter-out $(OVERRIDE_TARGETS), subdirs.clean): $(SUBDIRS:%=%.clean) ;
$(filter-out $(OVERRIDE_TARGETS), subdirs.install): $(SUBDIRS:%=%.install) ;
$(filter-out $(OVERRIDE_TARGETS), subdirs.uninstall): $(SUBDIRS:%=%.uninstall) ;

$(filter-out $(OVERRIDE_TARGETS), $(SUBDIRS:%=%.all)): %.all: force 
	$(MAKE) -C $* all
$(filter-out $(OVERRIDE_TARGETS), $(SUBDIRS:%=%.clean)): %.clean: force
	$(MAKE) -C $* clean
$(filter-out $(OVERRIDE_TARGETS), $(SUBDIRS:%=%.install)): %.install: force
	$(MAKE) -C $* install
$(filter-out $(OVERRIDE_TARGETS), $(SUBDIRS:%=%.uninstall)): %.uninstall: force
	$(MAKE) -C $* uninstall
endif

# Special Subdirectory manipulation ############################## #

$(filter-out $(OVERRIDE_TARGETS), subdirs): $(SUBDIRS) ;
$(filter-out $(OVERRIDE_TARGETS), subdirs.subdist): $(SUBDIRS:%=%.subdist) ;
$(filter-out $(OVERRIDE_TARGETS), subdirs.dist): $(SUBDIRS:%=%.dist) ;
$(filter-out $(OVERRIDE_TARGETS), subdirs.undist): $(SUBDIRS:%=%.undist) ;

$(filter-out $(OVERRIDE_TARGETS), $(SUBDIRS)): force
	$(MAKE) -C $@
$(filter-out $(OVERRIDE_TARGETS), $(SUBDIRS:%=%.subdist)): %.subdist: force
	$(MAKE) -C $* subdist DISTDIR=$(DISTDIR)/$* SHAREDISTDIR=$(SHAREDISTDIR)/..

$(filter-out $(OVERRIDE_TARGETS), dist): subdist
	if [ -s $(DISTDIR)/$(SHAREDISTDIR)/vars.make ]; then \
	    mv $(DISTDIR)/$(SHAREDISTDIR)/vars.make $(DISTDIR)/$(SHAREDISTDIR)/vars.make.in ; fi
	    
	tar zcf $(NAME)-$(MAJOR).$(MINOR).$(REVISION).tar.gz $(NAME)-$(MAJOR).$(MINOR).$(REVISION)
	rm -rf $(NAME)-$(MAJOR).$(MINOR).$(REVISION)

.PHONY: force
force: ;

# vars.make autogeneration support ########################### #

$(SHARE)/vars.make:
	cp $(SHARE)/vars.make.in $(SHARE)/vars.make
